package com.stn.zjx.givingtreasure.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import java.io.File;

public class OpenLocalMapUtil {
    /**
     * 地图应用是否安装
     *
     * @return
     */
    public static boolean isGdMapInstalled() {
        return isInstallPackage("com.autonavi.minimap");
    }

    public static boolean isBaiduMapInstalled() {
        return isInstallPackage("com.baidu.BaiduMap");
    }

    private static boolean isInstallPackage(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }

    /**
     * 百度地图定位经纬度转高德经纬度
     *
     * @param bd_lat
     * @param bd_lon
     * @return
     */
    public static double[] bdToGaoDe(double bd_lat, double bd_lon) {
        double[] gd_lat_lon = new double[2];
        double PI = 3.14159265358979324 * 3000.0 / 180.0;
        double x = bd_lon - 0.0065, y = bd_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * PI);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * PI);
        gd_lat_lon[0] = z * Math.cos(theta);
        gd_lat_lon[1] = z * Math.sin(theta);
        return gd_lat_lon;
    }
    /**
     * 高德地图定位经纬度转百度经纬度
     * @param gd_lon
     * @param gd_lat
     * @return
     */
    public static double[] gaoDeToBaidu(double gd_lon, double gd_lat) {
        double[] bd_lat_lon = new double[2];
        double PI = 3.14159265358979324 * 3000.0 / 180.0;
        double x = gd_lon, y = gd_lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * PI);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * PI);
        bd_lat_lon[0] = z * Math.cos(theta) + 0.0065;
        bd_lat_lon[1] = z * Math.sin(theta) + 0.006;
        return bd_lat_lon;
    }
    public static double[] map_bd2tx(double lat, double lon){
        double[] map_lat_lon = new double[2];
        double tx_lat;
        double tx_lon;
        double x_pi=3.14159265358979324;
        double x = lon - 0.0065, y = lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        tx_lon = z * Math.cos(theta);
        tx_lat = z * Math.sin(theta);
        map_lat_lon[0]=tx_lon;
        map_lat_lon[1]=tx_lat;
        return map_lat_lon;
    }
        /**
         * 打开高德地图,如果有打开，否则网页版
         */
    public static void openGaoDeMap(Context context,String appname,String fromLat,String fromLon, String toLat,String toLon) {
        if (OpenLocalMapUtil.isGdMapInstalled()) {
            Intent intent1 = new Intent();
            intent1.setAction(Intent.ACTION_VIEW);
            intent1.addCategory(Intent.CATEGORY_DEFAULT);
            //将功能Scheme以URI的方式传入data
            Uri uri = Uri.parse("androidamap://navi?" +
                    "sourceApplication=" + appname +"&lat=" + toLat + "&lon=" + toLon + "&dev=1&style=2");
            intent1.setData(uri);
            context.startActivity(intent1);
        }else{
            System.out.println("http://uri.amap.com/navigation?from=" +fromLon + "," + fromLat + "&to="+ toLon + "," + toLat + "&src=myapp");
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.setData(Uri.parse("http://uri.amap.com/navigation?from=" +fromLon + "," + fromLat + "&to="+ toLon + "," + toLat + "&src=myapp"));
            context.startActivity(intent); // 启动调用

        }
    }
    /**
     * 打开百度地图,如果有打开，否则网页版
     */
    public static void openBaiDuMap(String fromAddress,String toAddress,Context context,String appname,String fromLon,String fromLat, String toLon, String toLat) {
        if (OpenLocalMapUtil.isBaiduMapInstalled()) {
            Intent intent = new Intent();
            // 驾车导航
            intent.setData(Uri.parse("baidumap://map/navi?location=" +toLat+","+toLon+"&type=TIME&src=thirdapp.navi.hndist.sydt#Intent;scheme=bdapp;" +
                    "package=com.baidu.BaiduMap;end"));
            context.startActivity(intent);
        }else{
            String uri = "http://map.baidu.com/?latlng="+toLat+","+toLat+"&title=我的位置&content="+toAddress+"&autoOpen=true";
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.setData(Uri.parse(uri));
            context.startActivity(intent);

        }
    }

    public static void openTencentMap(Context context,String appname,String fromAddress,String toAddress,String fromLon,String fromLat, String toLon, String toLat){
        String url = "http://apis.map.qq.com/uri/v1/routeplan?type=drive&from="+fromAddress+"&fromcoord="
                +fromLon + "," + fromLat
                + "&to="+toAddress+"&tocoord="+toLon + "," + toLat  + "&policy=driving&referer=myapp";
        // drive --- 驾车 -------policy=0 是较快捷
        Uri tencentUri = Uri.parse(url);
        Intent loction = new Intent(Intent.ACTION_VIEW, tencentUri);
        context. startActivity(loction);

    }

}
